﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DatabaseCompare.Model;
using DatabaseCompare.Service.Compare;

namespace DatabaseCompare.ServiceImpl.Compare
{

    public class ViewCompare :
        CompareAbleCompare<ViewInfo>, 
        ICompareAbleCompare<ViewInfo, ViewCompareResult>
    {


        /// <summary>
        /// 列List的比较类.
        /// </summary>
        private ICompareAbleListCompare<ColumnInfo, ColumnCompareResult> columnListCompare = new ColumnListCompare();




        public ViewCompareResult Compare(ViewInfo leftView, ViewInfo rightView)
        {
            // 比较结果.
            ViewCompareResult result = new ViewCompareResult();

            // 设置比较参数.
            result.Left = leftView;
            result.Right = rightView;

            // 首先简单作非空的判断.
            result.CompareResult = base.CommonCompare(leftView, rightView);

            if (result.CompareResult == CommonCompareResult.FullSame)
            {
                // 左右都不为空
                // 需要详细比较.

                // 备注/说明信息
                if (leftView.Comment != rightView.Comment)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.CommentDifference = true;
                }

                // 创建视图的SQL语句信息
                if (leftView.CreateViewSQL != rightView.CreateViewSQL)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.CreateViewSQLDifference = true;
                }

                // 视图比较结束了，开始比较列.
                result.ColumnCompareResultList =
                    columnListCompare.Compare(leftView.SubList, rightView.SubList);

                // 比较列完毕后，只要列里面，有一个不完全匹配
                // 那么认为视图不匹配.
                if (result.ColumnCompareResultList != null
                    && result.ColumnCompareResultList.Count > 0)
                {
                    if (result.ColumnCompareResultList.Count(p => p.CompareResult != CommonCompareResult.FullSame) > 0)
                    {
                        result.CompareResult = CommonCompareResult.Difference;
                    }
                }

            }


            return result;
        }
    }

}
